# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/6/11 17:28
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : reg_addr_parser.py
# @Version: planner_builder 1.0


import xml.etree.ElementTree as EleTree


class RegAddr:
    def __init__(self):
        # 参数配置表
        self.rc_threshold = None
        self.hpriority_policing_threshold = None
        self.lpriority_policing_threshold = None
        self.qbv_or_qch = None
        self.time_slot_len = None
        self.schedule_period = None
        self.token_bucket_size = None
        self.add_tokens_num_perround = None
        self.forward_mode = None
        self.cycle_start_cyclen = None
        # 转发表
        self.fwd_tbl = None
        # GCL表
        self.gcl_tbl = None
        # 映射表
        self.map_tbl = None
        # 注入时刻表
        self.inject_tbl = None
        # 时间通告表
        self.inform_tbl = None
        # DMAC转发表
        self.mac_tbl = None
        # 防止广播风暴的标识
        self.broadcast_flag = None
        # 控制索引表
        self.index_tbl = None
        # 消除索引表
        self.etc_tbl = None
        # test_aux表
        self.aux_tbl = None
        # 转发模式表
        self.fwd_mode_tbl = None

    # 初始化一个RegAddr之后，调用该方法
    # 这个方法读取addr_define.xml中的信息
    def setup_reg_addr(self, xml):
        tree = EleTree.parse(xml)

        tag_set = ['rc_threshold',
                   'hpriority_policing_threshold',
                   'lpriority_policing_threshold',
                   'qbv_or_qch',
                   'committed_burst_size',
                   'committed_information_rate',
                   'time_slot_len',
                   'schedule_period',
                   'cyclelen']

        text_set = [0] * len(tag_set)

        cnt = 0
        for tag in tag_set:
            text = tree.iter(tag)
            text = list(text)[0].text
            text = int(text, 16)
            # text = host_to_net(text)
            text_set[cnt] = text
            cnt += 1

        self.rc_threshold = {'addr': text_set[0], 'width': 1}
        self.hpriority_policing_threshold = {'addr': text_set[1], 'width': 1}
        self.lpriority_policing_threshold = {'addr': text_set[2], 'width': 1}
        self.qbv_or_qch = {'addr': text_set[3], 'width': 1}
        self.token_bucket_size = {'addr': text_set[4], 'width': 1}
        self.add_tokens_num_perround = {'addr': text_set[5], 'width': 1}
        self.time_slot_len = {'addr': text_set[6], 'width': 1}
        self.schedule_period = {'addr': text_set[7], 'width': 1}
        # 下列几个寄存器的宽度不清楚
        self.cycle_start_cyclen = {'addr': text_set[8], 'width': 1}

        tag_set = ['flowid_forward_table_addr',
                   'TX_FL-tag_mapping_table_addr',
                   'inject_time_table_addr',
                   'time_informing_table_addr',
                   'mac_table_addr',
                   'broadcast_flag_addr',
                   'index_table_addr',
                   'etc_table_addr',
                   'forward_mode_table_addr']
        text_set = [0] * len(tag_set)

        cnt = 0
        for tag in tag_set:
            ele = tree.find(tag)
            text = ele.findtext('base_addr')
            text = int(text, 16)
            # text = host_to_net(text)
            text_set[cnt] = text
            cnt += 1

        self.fwd_tbl = {'addr': text_set[0], 'width': 2}
        self.map_tbl = {'addr': text_set[1], 'width': 8}
        self.inject_tbl = {'addr': text_set[2], 'width': 1}
        self.inform_tbl = {'addr': text_set[3], 'width': 4}
        self.mac_tbl = {'addr': text_set[4], 'width': 4}
        self.broadcast_flag = {'addr': text_set[5], 'width': 2}
        self.index_tbl = {'addr': text_set[6], 'width': 1}
        self.etc_tbl = {'addr': text_set[7], 'width': 4}
        self.fwd_mode_tbl = {'addr': text_set[8], 'width': 2}

        ele_set = tree.find('gate_control_list_table_addr')
        ele_set = list(ele_set)
        gcl_set = []
        for ele in ele_set:
            text = ele.text
            text = int(text, 16)
            # text = host_to_net(text)
            gcl = {'addr': text, 'width': 1}
            gcl_set.append(gcl)
        self.gcl_tbl = gcl_set

        # 下面开始是aux表的数据
        tag_set = ['base_addr',
                   'stream_identification_table']
        text_set = [0] * len(tag_set)

        ele_set = tree.find('test_aux_addr')
        ele_set = list(ele_set)

        cnt = 0
        for ele in ele_set:
            text = ele.text
            text = int(text, 16)
            # text = host_to_net(text)
            text_set[cnt] = text
            cnt += 1

        aux_set = []
        aux = {'addr': text_set[0], 'width': 12}
        aux_set.append(aux)
        aux = {'addr': text_set[1], 'width': 16}
        aux_set.append(aux)
        self.aux_tbl = aux_set



def main():
    xml = "../conf/addr_define.xml"
    reg_addr = RegAddr()
    reg_addr.setup_reg_addr(xml)


if __name__ == '__main__':
    main()
